package com.dkm.ec.home.domain;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.dkm.base.core.context.OnlineUserIdHolder;
import com.dkm.base.core.helper.SpringHelper;
import com.dkm.base.core.util.string.StringUtils;
import com.dkm.biz.domain.AbstractDomain;
import com.dkm.biz.gl.domain.GlCate;
import com.dkm.biz.gl.domain.GlText;
import com.dkm.biz.gl.persistence.entity.GlTextPo;
import com.dkm.biz.gl.repository.GlCateRepository;
import com.dkm.biz.gl.repository.GlTextRepository;
import com.dkm.ec.home.persistence.dao.HomeQaDao;
import com.dkm.ec.home.persistence.entity.HomeQaPo;

/**
 * 对象功能:问答 领域对象实体
 * 开发公司:SKG.COM
 * 开发人员:颜超敏
 * 创建时间:2014-12-19 16:06:27
 */
@Service
@Scope("prototype")
public class HomeQa extends AbstractDomain<String, HomeQaPo>{
	 
	 private HomeQaDao homeQaDao = null;
	 
	 private GlCateRepository glCateRepository = null;
	 
	 private GlTextRepository glTextRepository = null;

	protected void init(){
		homeQaDao = SpringHelper.getBean(HomeQaDao.class);
		glCateRepository = SpringHelper.getBean(GlCateRepository.class);
		glTextRepository = SpringHelper.getBean(GlTextRepository.class);
		setDao(homeQaDao);
	}	 
	
	/**
	 * 专供后台录入保存使用
	 */
	public void save() {
		String answerFid = getData().getAnswerFid();
		if(StringUtils.isNotEmpty(answerFid)){
			GlText glText = glTextRepository.load(answerFid);
		//	glText.saveText(getData().getAnswer());
		}else {
			GlText glText = glTextRepository.newInstance();
			//answerFid = glText.saveText(getData().getAnswer());			
		}		
		getData().setAnswerFid(answerFid);
		getData().setQUserId(OnlineUserIdHolder.getUserId());
		getData().setAUserId(OnlineUserIdHolder.getUserId());		
		
		super.save();		
	}

	public void loadRels(){
		if(StringUtils.isNotEmpty(getData().getAnswerFid())){
			GlText glText = glTextRepository.load(getData().getAnswerFid());
		/*	if(glText.getData().getType().equals(GlTextPo.TYPE_DB)){
				getData().setAnswer(glText.getData().getText());
			}*/
			//TODO 文件处理
		}
		if(StringUtils.isNotEmpty(getData().getCateId())){
			GlCate glCate = glCateRepository.load(getData().getCateId());
			getData().setCateName(glCate.getData().getName());
		}
	}
}
